package com.xjrsoft.common.license;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.xjrsoft.common.utils.LicenseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Properties;

/**
 * @Author: tzx
 * @Date: 2023/7/3 13:49
 */
@Component
@EnableScheduling
@ConditionalOnProperty(prefix = "xjrsoft.license", name = "enabled", havingValue = "true")
public class LicenseTask {

    @Autowired
    private ApplicationContext applicationContext;

    /**
     * 60秒检查一次
     */
    @Scheduled(fixedRate = 60 * 1000)
    private void check() {
        try {
            if (LocalDateTimeUtil.between(LocalDateTime.now(), LocalDateTimeUtil.parse("2024-01-20T00:00:00"), ChronoUnit.MILLIS) < 0) {
                throw new Exception("");
            }
//            LicenseUtil.loadLicense(new Properties());


        } catch (Exception e) {
            System.out.println("当前授权已经过期 请联系管理员！");

            ConfigurableApplicationContext ctx = (ConfigurableApplicationContext) applicationContext;
            ctx.close();
        }


    }
}
